package com.fieldchain.ex.fa100.ui.personal.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;

import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.UpdateUser;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.fieldchain.ex.fa100.utils.version.VersionUtils;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.ProductDetail;
import com.qiyukf.unicorn.api.Unicorn;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/11
 */
public class PersonalCanterActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvLogin;
    private LinearLayout linUser;
    private TextView tvUid;
    private TextView tvName;
    private RelativeLayout reaUserinfo;
    private RelativeLayout reaSafe;
    private RelativeLayout reaMoneytype;
    private TextView tvMoneyType;
    private RelativeLayout reaInvite;
    private TextView tvVersion;
    private RelativeLayout reaService;
    private TextView tvLoginout;

    private User user;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvLogin = findViewById(R.id.tv_login);
        linUser = findViewById(R.id.lin_user);
        tvUid = findViewById(R.id.tv_uid);
        tvName = findViewById(R.id.tv_name);
        reaUserinfo = findViewById(R.id.rea_userinfo);
        reaSafe = findViewById(R.id.rea_safe);
        reaMoneytype = findViewById(R.id.rea_moneytype);
        tvMoneyType = findViewById(R.id.tv_moneyType);
        reaInvite = findViewById(R.id.rea_invite);
        tvVersion = findViewById(R.id.tv_version);
        reaService = findViewById(R.id.rea_service);
        tvLoginout = findViewById(R.id.tv_loginout);
    }

    @Override
    public void setValue() {
        tvCenter.setText("个人中心");
        reaBack.setOnClickListener(this);
        reaUserinfo.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        reaSafe.setOnClickListener(this);
        reaMoneytype.setOnClickListener(this);
        reaInvite.setOnClickListener(this);
        reaService.setOnClickListener(this);
        tvLoginout.setOnClickListener(this);
        reaMoneytype.setOnClickListener(this);
        tvVersion.setText(VersionUtils.getLocalVersionName());
        reaInvite.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UpdateUser.update(new IntegerCallBack() {
            @Override
            public void index(int status) {
                if(status==200) {
                    tvLogin.setVisibility(View.GONE);
                    linUser.setVisibility(View.VISIBLE);
                    tvLoginout.setVisibility(View.VISIBLE);
                    userShow();
                }else{
                    tvLogin.setVisibility(View.VISIBLE);
                    linUser.setVisibility(View.GONE);
                    tvLoginout.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_personal;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() != R.id.rea_back && view.getId() != R.id.tv_login) {
            if (user == null) {
                MyToast.show("请先登录");
                return;
            }
        }
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.tv_login:
                startActivity(new Intent(mContext, LoginActivity.class));
                break;
            case R.id.rea_userinfo:
                startActivity(new Intent(mContext, UserSetActivity.class));
                break;
            case R.id.tv_loginout:
                tvLogin.setVisibility(View.VISIBLE);
                linUser.setVisibility(View.GONE);
                tvLoginout.setVisibility(View.GONE);
                ShareManager.getInstance().clearUser();
                ShareManager.getInstance().clearCookei();
                loginOut();
                break;
            case R.id.rea_safe:
                startActivity(new Intent(mContext, SafeActivity.class));
                break;
            case R.id.rea_moneytype:
                if(user.getAssistantCurrency()==1){
                    startActivity(new Intent(mContext, AuxiliaryUSDActivity.class));
                }else{
                    startActivity(new Intent(mContext, AuxiliaryMoneyActivity.class));
                }

                break;
            case R.id.rea_invite:
                startActivity(new Intent(mContext, InviteActivity.class));
                break;
            case R.id.rea_service:
                MyToast.show("暂未开放");
//                consultService(mContext, "", "艾场客服", null);
                break;
        }
    }

    public void userShow() {
        user = ShareManager.getInstance().getUser();

        if (user != null) {
            String phone=user.getPhone();
            String email=user.getEmail();
            String name=phone!=null?phone:email;
            tvName.setText(user.getTrueName());
            tvUid.setText("UID:"+user.getId());
            tvName.setText(name);
            String moneyType=user.getAssistantCurrency()==1?"USD":"CNY";
            Log.e("moneyType",moneyType);
            tvMoneyType.setText(moneyType);
            tvLogin.setVisibility(View.GONE);
            linUser.setVisibility(View.VISIBLE);

        }else{
            tvLogin.setVisibility(View.VISIBLE);
            linUser.setVisibility(View.GONE);
        }
    }

    public void loginOut(){
        HttpRequest.getInstance().getRequest(Constant.loginout,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
    private void consultService(final Context context, String uri, String title, ProductDetail productDetail) {
        // 启动聊天界面
        ConsultSource source = new ConsultSource(uri, title, null);
        source.productDetail = productDetail;
        Unicorn.openServiceActivity(context, title, source);

    }

    @Override
    public void loginShow() {

    }
}
